<template>
  <CollapseContainer title="基本设置" :canExpan="false">
    <a-row :gutter="24">
      <a-col :span="14">
        <BasicForm @register="register" />
      </a-col>
      <a-col :span="10">
        <div class="change-avatar">
          <div class="mb-2">头像</div>
          <CropperAvatar
            :uploadApi="uploadApi"
            :value="avatar"
            btnText="更换头像"
            :btnProps="{ preIcon: 'ant-design:cloud-upload-outlined' }"
            @change="updateAvatar"
            width="150"
          />
        </div>
      </a-col>
    </a-row>
    <Button type="primary" @click="handleSubmit"> 更新基本信息 </Button>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Button, Row, Col } from 'ant-design-vue';
  import { ref, computed, defineComponent } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { CollapseContainer } from '/@/components/Container';
  import { CropperAvatar } from '/@/components/Cropper';
  import { useMessage } from '/@/hooks/web/useMessage';
  import headerImg from '/@/assets/images/header.jpg';
  import { accountInfoApi, updateAccountUserInfo } from '/@/api/sys/account';
  import { simpleFileMinioUploadApi } from '/@/api/business/file';
  import { baseSetschemas } from './data';
  import { useUserStore } from '/@/store/modules/user';
  import { minioUploadApi } from '/@/api/business/file';
  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
      Button,
      ARow: Row,
      ACol: Col,
      CropperAvatar,
    },
    setup() {
      const { createMessage } = useMessage();
      const userStore = useUserStore();
      const uploadApiUri = ref('');
      const [register, { setFieldsValue, getFieldsValue }] = useForm({
        labelWidth: 120,
        schemas: baseSetschemas,
        showActionButtonGroup: false,
      });

      const avatar = computed(() => {
        const { avatar } = userStore.getUserInfo;
        return avatar || headerImg;
      });

      const uploadApi = (params: UploadFileParams) => {
        minioUploadApi(uploadApiUri.value, params.file);
      };

      const updateAvatar = async () => {
        const data = await accountInfoApi();
        //头像上传成功后 将头像地址赋值用户信息
        // 这个地方应该是配置的 偷个懒
        const userDetail = getFieldsValue();
        const avurl = 'http://localhost:9000/filewarehouse/profilePhoto' + data.userId;
        userDetail.avatar = avurl;
        updateAccountUserInfo({ ...userDetail });

        userStore.getUserInfoAction();
      };

      const handleSubmit = () => {
        updateAccountUserInfo(getFieldsValue());
        createMessage.success('更新成功！');
      };

      const init = async () => {
        const data = await accountInfoApi();
        setFieldsValue(data);
        // 获取头像上传地址
        const data2 = await simpleFileMinioUploadApi({
          objectName: 'profilePhoto' + data.userId,
        });
        uploadApiUri.value = data2;
      };

      return {
        init,
        avatar,
        register,
        updateAvatar,
        uploadApi,
        handleSubmit,
      };
    },
    mounted() {
      this.init();
    },
  });
</script>

<style lang="less" scoped>
  .change-avatar {
    img {
      display: block;
      margin-bottom: 15px;
      border-radius: 50%;
    }
  }
</style>
